<?php
/**
 * EverBox Theme Customizer
 *
 * @package EverBox
 */

/**
 * Adjusting color
 * @param  [type] $hex   color need to be adjusted
 * @param  [type] $steps adjust by step
 * @return [type]        hex color
 */
function everbox_adjustBrightness($hex, $steps) {
    // Steps should be between -255 and 255. Negative = darker, positive = lighter
    $steps = max(-255, min(255, $steps));

    // Normalize into a six character long hex string
    $hex = str_replace('#', '', $hex);
    if (strlen($hex) == 3) {
        $hex = str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2);
    }

    // Split into three parts: R, G and B
    $color_parts = str_split($hex, 2);
    $return = '#';

    foreach ($color_parts as $color) {
        $color   = hexdec($color); // Convert to decimal
        $color   = max(0,min(255,$color + $steps)); // Adjust color
        $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code
    }

    return $return;
}
/**
 *  The Output for theme customize
 */
function everbox_custom_footer() {
	global $everbox_options;
	$primary_color = isset($everbox_options['primary_color']) ? $everbox_options['primary_color'] : '#57ad68';
	$body_code = isset($everbox_options['body_code']) ? $everbox_options['body_code'] : '';
?>
	<style>
	a {color: <?php echo $primary_color ?>;}
	a:hover {color: <?php echo everbox_adjustBrightness($primary_color, -25); ?>;}
	::selection {background-color: <?php echo $primary_color ?>;color: #fff;}
	ul.main-nav > li.current-menu-item > a:first-child {background-color: <?php echo $primary_color ?>;}
	.archive-header .archive-title, .search-header .search-title {border-color: <?php echo $primary_color ?>;}
	.post-meta.category-links a:hover {background-color: <?php echo $primary_color ?>;}
	.post-meta a:hover {color: <?php echo $primary_color ?>;}
	.post-meta .author {color: <?php echo $primary_color ?>;}
	.pagination .current {background-color: <?php echo $primary_color ?>;border-color: <?php echo $primary_color ?>;}
	.comments-area .comment-header .comment-title {border-color: <?php echo $primary_color ?>;}
	.comments-list li .comment-body .comment-heading .comment-reply-link:hover {color: <?php echo $primary_color ?>;}
	.comments-list li .comment-body .comment-byauthor {background: <?php echo $primary_color ?> !important;}
	.filter-button.active {color: <?php echo $primary_color ?>;}
	.filter-button.active a {color: <?php echo $primary_color ?>;}
	.widget .widget-head .widgettitle, .widget .widget-head .widget-title {border-color: <?php echo $primary_color ?>;}
	.tagcloud a:hover, .page-tags a:hover {background-color: <?php echo $primary_color ?>;border-color: <?php echo $primary_color ?>;}
	#wp-calendar tbody td a {background: <?php echo $primary_color ?>}
	#wp-calendar tbody td:hover a {background: <?php echo everbox_adjustBrightness($primary_color, 15); ?>}
	#wp-calendar caption {background: <?php echo $primary_color ?>}
	.link-pages > span {background-color: <?php echo $primary_color ?>;border-color: <?php echo $primary_color ?>; }
	.link-pages > span:hover {background-color: <?php echo $primary_color ?>;}
	.pagination .current {background-color: <?php echo $primary_color ?>;border-color: <?php echo $primary_color ?>;}
	.pagination .current:hover {background-color: <?php echo $primary_color ?>;}
	@media (device-width: 375px) and (height: 667px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2) {
		.site-header {
			background: <?php echo $primary_color ?>;
		}
		.mobile-menu-container, .mobile-search-container {
			background: <?php echo everbox_adjustBrightness($primary_color, -15); ?>;
		}
	}
	</style>
	<?php echo $body_code; ?>
<?php
}
add_action('wp_footer', 'everbox_custom_footer' );

function everbox_custom_head() {
	global $everbox_options;
	
	$head_code = isset($everbox_options['head_code']) ? $everbox_options['head_code'] : '';

	$favicon_url = !empty($everbox_options['favicon']) ? $everbox_options['favicon'] : EVERBOX_URL . '/images/favicon.ico';
	$ipad_retina_icon152 = $everbox_options['ipad_retina_icon152'];
	$ipad_retina_icon144 = $everbox_options['ipad_retina_icon144'];
	$ipad_icon76 = $everbox_options['ipad_icon76'];
	$ipad_icon72 = $everbox_options['ipad_icon72'];
	$iphone_retina_icon180 = $everbox_options['iphone_retina_icon180'];
	$iphone_retina_icon120 = $everbox_options['iphone_retina_icon120'];
	$iphone_retina_icon114 = $everbox_options['iphone_retina_icon114'];
	$iphone_icon57 = $everbox_options['iphone_icon57'];

	$ipad_startup1536 = $everbox_options['ipad_startup1536'];
	$ipad_startup1496 = $everbox_options['ipad_startup1496'];
	$ipad_startup768 = $everbox_options['ipad_startup768'];
	$ipad_startup748 = $everbox_options['ipad_startup748'];
	$iphone_startup1242 = $everbox_options['iphone_startup1242'];
	$iphone_startup1182 = $everbox_options['iphone_startup1182'];
	$iphone_startup750 = $everbox_options['iphone_startup750'];
	$iphone_startup640 = $everbox_options['iphone_startup640'];
	$iphone_startup920 = $everbox_options['iphone_startup920'];
	$iphone_startup320 = $everbox_options['iphone_startup320'];
?>
	<!--[if lt IE 9]>
    <script src="<?php echo EVERBOX_URL . '/js/' ?>html5shiv-printshiv.js"></script>
  	<![endif]-->
	<!-- Set the viewport. -->
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<!-- Allow web app to be run in full-screen mode. -->
	<meta content="yes" name="apple-mobile-web-app-capable" />
	<!-- Make the app title different than the page title. -->
	<meta name="apple-mobile-web-app-title" content="<?php bloginfo('name'); ?>">
	<meta name="apple-mobile-web-app-capable" content="yes">
	 <!-- Configure the status bar. -->
	<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
	<!-- Disable automatic phone number detection. -->
	<meta name="format-detection" content="telephone=no">

	<!-- ICONS -->
	<link href="<?php echo $favicon_url; ?>" rel="icon">
	<?php if(isset($ipad_retina_icon152) && !empty($ipad_retina_icon152)) : ?>
	<!-- iPad retina icon -->
    <link href="<?php echo $ipad_retina_icon152; ?>" sizes="152x152" rel="apple-touch-icon-precomposed">
    <?php endif; ?>
    <?php if(isset($ipad_retina_icon144) && !empty($ipad_retina_icon144)) : ?>
    <!-- iPad retina icon (iOS < 7) -->
    <link href="<?php echo $ipad_retina_icon144; ?>" sizes="144x144" rel="apple-touch-icon-precomposed">
    <?php endif; ?>
    <?php if(isset($ipad_icon76) && !empty($ipad_icon76)) : ?>
    <!-- iPad non-retina icon -->
    <link href="<?php echo $ipad_icon76; ?>" sizes="76x76" rel="apple-touch-icon-precomposed">
    <?php endif; ?>
    <?php if(isset($ipad_icon72) && !empty($ipad_icon72)) : ?>
    <!-- iPad non-retina icon (iOS < 7) -->
    <link href="<?php echo $ipad_icon72; ?>" sizes="72x72" rel="apple-touch-icon-precomposed">
   	<?php endif; ?>
   	<?php if(isset($iphone_retina_icon180) && !empty($iphone_retina_icon180)) : ?>
    <!-- iPhone 6 Plus icon -->
    <link href="<?php echo $iphone_retina_icon180; ?>" sizes="180x180" rel="apple-touch-icon-precomposed">
   	<?php endif; ?>
   	<?php if(isset($iphone_retina_icon120) && !empty($iphone_retina_icon120)) : ?>
    <!-- iPhone 5/6 icon -->
    <link href="<?php echo $iphone_retina_icon120; ?>" sizes="120x120" rel="apple-touch-icon-precomposed">
   	<?php endif; ?>
   	<?php if(isset($iphone_retina_icon114) && !empty($iphone_retina_icon114)) : ?>
    <!-- iPhone retina icon (iOS < 7) -->
    <link href="<?php echo $iphone_retina_icon114; ?>" sizes="114x114" rel="apple-touch-icon-precomposed">
    <?php endif; ?>
    <?php if(isset($iphone_icon57) && !empty($iphone_icon57)) : ?>
    <!-- iPhone non-retina icon (iOS < 7) -->
    <link href="<?php echo $iphone_icon57; ?>" sizes="57x57" rel="apple-touch-icon-precomposed">
	<?php endif; ?>
	
	<!-- STARTUP IMAGES -->
	<?php if(isset($ipad_startup1536) && !empty($ipad_startup1536)) : ?>
    <!-- iPad retina portrait startup image -->
    <link href="<?php echo $ipad_startup1536; ?>"
          media="(device-width: 768px) and (device-height: 1024px)
                 and (-webkit-device-pixel-ratio: 2)
                 and (orientation: portrait)"
          rel="apple-touch-startup-image">
	<?php endif; ?>

	<?php if(isset($ipad_startup1496) && !empty($ipad_startup1496)) : ?>
    <!-- iPad retina landscape startup image -->
    <link href="<?php echo $ipad_startup1496; ?>"
          media="(device-width: 768px) and (device-height: 1024px)
                 and (-webkit-device-pixel-ratio: 2)
                 and (orientation: landscape)"
          rel="apple-touch-startup-image">
	<?php endif; ?>

	<?php if(isset($ipad_startup768) && !empty($ipad_startup768)) : ?>
    <!-- iPad non-retina portrait startup image -->
    <link href="<?php echo $ipad_startup768; ?>"
          media="(device-width: 768px) and (device-height: 1024px)
                 and (-webkit-device-pixel-ratio: 1)
                 and (orientation: portrait)"
          rel="apple-touch-startup-image">
	<?php endif; ?>

	<?php if(isset($ipad_startup748) && !empty($ipad_startup748)) : ?>
    <!-- iPad non-retina landscape startup image -->
    <link href="<?php echo $ipad_startup748; ?>"
          media="(device-width: 768px) and (device-height: 1024px)
                 and (-webkit-device-pixel-ratio: 1)
                 and (orientation: landscape)"
          rel="apple-touch-startup-image">
	<?php endif; ?>
	
	<?php if(isset($iphone_startup1242) && !empty($iphone_startup1242)) : ?>
    <!-- iPhone 6 Plus portrait startup image -->
    <link href="<?php echo $iphone_startup1242; ?>"
          media="(device-width: 414px) and (device-height: 736px)
                 and (-webkit-device-pixel-ratio: 3)
                 and (orientation: portrait)"
          rel="apple-touch-startup-image">
	<?php endif; ?>

	<?php if(isset($iphone_startup1182) && !empty($iphone_startup1182)) : ?>
    <!-- iPhone 6 Plus landscape startup image -->
    <link href="<?php echo $iphone_startup1182; ?>"
          media="(device-width: 414px) and (device-height: 736px)
                 and (-webkit-device-pixel-ratio: 3)
                 and (orientation: landscape)"
          rel="apple-touch-startup-image">
	<?php endif; ?>

	<?php if(isset($iphone_startup750) && !empty($iphone_startup750)) : ?>
    <!-- iPhone 6 startup image -->
    <link href="<?php echo $iphone_startup750; ?>"
          media="(device-width: 375px) and (device-height: 667px)
                 and (-webkit-device-pixel-ratio: 2)"
          rel="apple-touch-startup-image">
	<?php endif; ?>

	<?php if(isset($iphone_startup640) && !empty($iphone_startup640)) : ?>
    <!-- iPhone 5 startup image -->
    <link href="<?php echo $iphone_startup640; ?>"
          media="(device-width: 320px) and (device-height: 568px)
                 and (-webkit-device-pixel-ratio: 2)"
          rel="apple-touch-startup-image">
	<?php endif; ?>

	<?php if(isset($iphone_startup920) && !empty($iphone_startup920)) : ?>
    <!-- iPhone < 5 retina startup image -->
    <link href="<?php echo $iphone_startup920; ?>"
          media="(device-width: 320px) and (device-height: 480px)
                 and (-webkit-device-pixel-ratio: 2)"
          rel="apple-touch-startup-image">
	<?php endif; ?>
	
	<?php if(isset($iphone_startup320) && !empty($iphone_startup320)) : ?>
    <!-- iPhone < 5 non-retina startup image -->
    <link href="<?php echo $iphone_startup320; ?>"
          media="(device-width: 320px) and (device-height: 480px)
                 and (-webkit-device-pixel-ratio: 1)"
          rel="apple-touch-startup-image">
	<?php endif; ?>

<?php echo $head_code; ?>
<?php
}
add_action('wp_head', 'everbox_custom_head');